<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register Page</title>
    <style>
        body {
            background-color: #eee;
        }

        .container {
            width: 400px;
            margin: 0 auto;
            margin-top: 100px;
            background-color: #fff;
            box-shadow: 0px 0px 5px #888;
            padding: 30px;
        }

        h1 {
            font-size: 2.5rem;
            text-align: center;
        }

        label {
            display: block;
            font-size: 1.2rem;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            border: none;
            border-bottom: 2px solid #ccc;
            font-size: 1.1rem;
            padding: 10px;
            margin-bottom: 20px;
            transition: border-bottom .3s ease-in-out;
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            border-bottom-color: #3498db;
        }

        button[type="submit"] {
            background-color: #3498db;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            font-size: 1.2rem;
            cursor: pointer;
        }

        button[type="submit"]:hover {
            background-color: #2775b5;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>注&nbsp;&nbsp;册</h1>
        <form>
            <label for="software">企业-软件名:</label>
            <select id="software" name="software">
                <option value="ComA-a1">A公司-软件a1</option>
                <option value="ComA-a2">A公司-软件a2</option>
                <option value="ComA-a3">A公司-软件a3</option>
                <option value="ComB-b1">B公司-软件b1</option>
                <option value="ComB-b2">B公司-软件b2</option>
            </select>
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" placeholder="起个名字吧">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" placeholder="设置个密码吧">
            <button type="submit">注册</button>
        </form>
        <div style="margin-top: 10px; text-align: center;">
            已有账号? <a href="./login.html">点我登录</a>
        </div>
    </div>
    <script>
        const form = document.getElementsByTagName('form')[0]
        form.addEventListener('submit', function (e) {
            e.preventDefault()
            e.preventDefault()
            const softselect = document.getElementById("software")
            const company_app = softselect.options[softselect.selectedIndex].value.split("-")
            const username = document.getElementById('username').value.trim()
            const password = document.getElementById('password').value.trim()
            if (softselect === '' || username === '' || password === '') {
                alert('Please enter your software name, username and password.')
            } else {
                const mes = {
                    company: company_app[0],
                    software: company_app[1],
                    username,
                    password,

                }
                fetch("http://localhost:5050/register", {
                    method: "post",
                    body: JSON.stringify(mes),
                    headers: {
                        "Content-Type": "application/json"
                    }
                }).then(res => {
                    window.location.href = './login.html';
                })
            }
        })
    </script>
</body>

</html>